// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2025-06-01
// @description  try to take over the world!
// @author       You
// @match        https://weibo.com/u/*
// @icon         
// @require     https://code.jquery.com/jquery-3.6.0.min.js
// @grant        GM_xmlhttpRequest
// @run-at      document-idle
// @connect      127.0.0.1
// ==/UserScript==

(function() {
    'use strict';
    // Create the main div element
    const currentUrl = window.location.href.split('/');  // 获取完整URL:ml-citation{ref="8" data="citationList"}
    const uid = currentUrl[currentUrl.length - 1] || '';
    console.log(uid);

    var mainDiv = document.createElement('div');
    mainDiv.style.position = 'fixed'; // Position the div fixed to stay in place
    mainDiv.style.left = '0';         // Position the div at the left side of the page
    mainDiv.style.top = '200px';       // Position the div slightly below the top (adjust as needed)
    mainDiv.style.width = '100px';    // Set the width to 100px
    mainDiv.style.height = '100px';   // Set the height to 100px
    mainDiv.style.backgroundColor = '#f0f0f0'; // Set a background color for visibility
    mainDiv.style.border = '1px solid #ccc';   // Add a border for better visibility
    mainDiv.style.boxSizing = 'border-box';    // Include padding and border in the element's total width and height
    mainDiv.style.display = 'flex';            // Use flexbox for vertical alignment of buttons
    mainDiv.style.flexDirection = 'column';    // Set flex direction to column for vertical alignment
    mainDiv.style.alignItems = 'center';       // Center align items vertically
    mainDiv.style.justifyContent = 'center';   // Center align items horizontally (within the flex direction)
    mainDiv.style.padding = '10px';            // Add some padding inside the div

    // Create the first button
    var button_fetch = document.createElement('button');
    button_fetch.textContent = '获取链接';
    button_fetch.id = 'button_fetch' + Math.random().toString(36).substr(2);
    button_fetch.style.margin = '5px 0'; // Add some margin between buttons

    // Create the second button
    var button_download_img = document.createElement('button');
    button_download_img.textContent = '下载图片';
    button_fetch.id = 'button_download_img' + Math.random().toString(36).substr(2);
    button_download_img.style.margin = '5px 0'; // Add some margin between buttons

    // Create the third button
    var button_download_video = document.createElement('button');
    button_download_video.textContent = '下载视频';
    button_fetch.id = 'button_download_video' + Math.random().toString(36).substr(2);
    button_download_video.style.margin = '5px 0'; // Add some margin between buttons (bottom margin in this case)

    // Append buttons to the main div
    mainDiv.appendChild(button_fetch);
    mainDiv.appendChild(button_download_img);
    mainDiv.appendChild(button_download_video);

    button_fetch.addEventListener('click', function() {
        button_fetch.textContent = '获取链接中...';
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://127.0.0.1:5000/fetch_web",
            headers: {"Content-Type": "application/json"},
            data: JSON.stringify({uid: uid}),
            onload: function(res) {
                console.log("GM_xmlhttpRequest成功:", res.responseText);
                button_fetch.textContent = '获取链接';
            },
            onerror: function(err) {
                console.error("GM_xmlhttpRequest失败:", err);
                button_fetch.textContent = '获取链接';
            }
        });
    });

    button_download_img.addEventListener('click', function() {
        button_download_img.textContent = '下载图片中...';
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://127.0.0.1:5000/download_img",
            headers: {"Content-Type": "application/json"},
            data: JSON.stringify({uid: uid}),
            onload: function(res) {
                console.log("GM_xmlhttpRequest成功:", res.responseText);
                button_download_img.textContent = '下载图片';
            },
            onerror: function(err) {
                console.error("GM_xmlhttpRequest失败:", err);
                button_download_img.textContent = '下载图片';
            }
        });
    });

    button_download_video.addEventListener('click', function() {
        button_download_video.textContent = '下载视频中...';
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://127.0.0.1:5000/download_video",
            headers: {"Content-Type": "application/json"},
            data: JSON.stringify({uid: uid}),
            onload: function(res) {
                console.log("GM_xmlhttpRequest成功:", res.responseText);
                button_download_video.textContent = '下载视频';
            },
            onerror: function(err) {
                console.error("GM_xmlhttpRequest失败:", err);
                button_download_video.textContent = '下载视频';
            }
        });
    });

    // Append the main div to the body of the page
    document.body.appendChild(mainDiv);
})();